package com.flyou.ccnucenter.ui.activity.henuBeside;

import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.widget.loading.LoadingView;

public class ScoreActivity extends BaseActivity1 implements View.OnClickListener {
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private WebView webView;
    private LoadingView mLoadView;
    private RelativeLayout failed_view;
    private Button reTry;
    private RelativeLayout correntlayout;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_score);
        correntlayout = (RelativeLayout) findViewById(R.id.correntlayout);
        failed_view = (RelativeLayout) findViewById(R.id.failed_view);
        reTry = (Button) findViewById(R.id.reTry);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        webView = (WebView) findViewById(R.id.henu_beside_score);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        mLoadView = (LoadingView) findViewById(R.id.loadingView);
        mLoadView.setLoadingText("正在拼命加载数……");
    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("成绩查询");
        //        设置webView
        mLoadView.setVisibility(View.VISIBLE);
        webView.setBackgroundColor(0);
        WebSettings webSettings = webView.getSettings();
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setBlockNetworkImage(false);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                mLoadView.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                mLoadView.setVisibility(View.GONE);
                Toast.makeText(ScoreActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                failed_view.setVisibility(View.VISIBLE);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

        mLoadView.setVisibility(View.VISIBLE);
        int score_type = getIntent().getIntExtra("score_type", 0);
        switch (score_type) {
            case 0:
                webView.loadUrl(Constant.HENU_SCOBESIDE_CET_SCORE_URL);
                break;
            case 1:
                webView.loadUrl(Constant.HENU_SCOBESIDE_CHINESE_SCORE_URL);
                break;
            case 2:
                webView.loadUrl(Constant.HENU_SCOBESIDE_CUMPUTER_SCORE_URL);
                break;
        }


    }


    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        reTry.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    finish();
                }
                break;
            case R.id.reTry:
                if (null != webView)
                    failed_view.setVisibility(View.GONE);
                webView.reload();
                break;
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (webView.canGoBack()) {
                webView.goBack();
            } else {
                finish();
            }

            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        correntlayout.removeView(webView);
        webView.removeAllViews();
        webView.destroy();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }


}
